import 'package:flutter/material.dart';
import 'package:lib_uikit/lib_uikit.dart';
import 'package:lib_uikit/widgets/tabbar/underline/gt_tabs.dart';

/// @Title   Secondary类型的tab
///   文字大小12，tab间隔8，tab高度24
/// @Author: bryce
/// @CreateDate: 2023年10月11日20:21:34
class GTSecondaryTabBar extends StatelessWidget {
  final List<String> tabTitles;
  final TabController? controller;
  final EdgeInsetsGeometry? padding;
  final ValueChanged<int>? onTap;

  // 新增tab切换拦截，如果返回true代表拦截，false允许切换
  final Future<bool> Function(TabController mController, int newIndex)? onInterceptSwitch;

  // 新增拦截点击tab时的切换（对于使用controller控制的和手势滚动TabBarView的切换不拦截）
  final Future<bool> Function(TabController mController, int newIndex)? onInterceptTapSwitch;

  const GTSecondaryTabBar({super.key, required this.tabTitles, this.controller, this.padding, this.onInterceptSwitch,this.onTap,this.onInterceptTapSwitch});

  @override
  Widget build(BuildContext context) {
    return GTTabBar(
        dividerColor: Colors.transparent,
        controller: controller,
        isScrollable: true,
        padding: padding,
        unselectedLabelColor: GTColorsV3.uikitText3V3.color,
        labelColor: GTColorsV3.uikitText1V3.color,
        labelStyle: gtTextStyleW600(fontSize: 12),
        unselectedLabelStyle: gtTextStyleW500(fontSize: 12),
        labelPadding: GTEdgeInsetsOnly(left: 8, right: 16),
        indicatorPadding: GTEdgeInsetsOnly(right: 8),
        onTap: onTap,
        overlayColor: MaterialStateProperty.resolveWith<Color?>(
          (Set<MaterialState> states) => Colors.transparent,
        ),
        onInterceptSwitch: onInterceptSwitch,
        onInterceptTapSwitch: onInterceptTapSwitch,
        indicator: BoxDecoration(color: GTColorsV3.uikitNeutral3V3.color, borderRadius: BorderRadius.circular(2)),
        tabs: tabs());
  }

  List<Widget> tabs() {
    return tabTitles.map((e) => _tabItem(e)).toList();
  }

  Widget _tabItem(String title) {
    return GTTab(
      height: 24,
      child: Text(title),
    );
  }
}
